<template>
    <div class="app-container">
        <el-form :inline="true" :model="searchQuery" class="demo-form-inline">
            <el-form-item label="课程类别">
                <el-select placeholder="请选择" v-model="searchQuery.subjectParentId" @change="subjectLevelOneChange">
                    <el-option v-for="item in subjectNestedList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select v-model="searchQuery.subjectId" placeholder="请选择">
                    <el-option v-for="item in subsubjectList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-input placeholder="课程标题" v-model="searchQuery.title"></el-input>
            </el-form-item>
            <el-form-item>
                <el-select v-model="searchQuery.teacherId" placeholder="请选择讲师">
                    <el-option v-for="item in teacherList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="getList()">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button plain @click="resetData">清空</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row row-class-name="myClassList">
            <el-table-column align="center" label="序号" width="70">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="课程信息" width="300">
                <template slot-scope="scope">
                    <!-- {{ scope.row.title }} -->
                    <div class="info">

                        <div class="pic">
                            <img :src="scope.row.cover" :alt="scope.row.title">
                        </div>
                        <div class="title">
                            <a href="#">{{ scope.row.title }}</a>
                            <p>{{ scope.row.lessonNum }}</p>
                        </div>
                    </div>

                </template>
            </el-table-column>
            <el-table-column label="创建时间" width="100" align="center">
                <template slot-scope="scope">
                    <span>{{ scope.row.gmtModified.substr(0, 10) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="发布时间" align="center" width="100">
                <template slot-scope="scope">
                    <span>{{ scope.row.gmtCreate.substr(0, 10) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="价格" align="center" width="100">
                <template slot-scope="scope">
                    <span>{{ scope.row.price === 0 ? '免费' : '￥' + scope.row.price.toFixed(2) }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="付费学员" width="100">
                <template slot-scope="scope">
                    <span>{{ scope.row.buyCount }}人</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="课程状态" width="150">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.status == 'Normal' ? 'success' : ''">
                        {{ scope.row.status == 'Normal' ? '已发布' : '未发布' }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <router-link :to="`/course/add/${scope.row.id}`">
                        <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
                    </router-link>
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteCourse(scope.row.id)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination style="text-align:center;margin-top: 20px;" @current-change="getList" :current-page="page"
            :page-size="limit" layout="total, prev, pager, next, jumper" :total="total">
        </el-pagination>
    </div>
</template>
<script>
import teacherApi from '@/api/teacher'
import courseApi from '@/api/course'
import subjectApi from '@/api/subject'
export default {
    data() {
        return {
            formInline: {},
            list: null,//当前列表所需的数据
            listLoading: true,//转菊花效果
            page: 1,//当前页
            limit: 10,//每页记录数
            searchQuery: {
                teacherId:"",
                subjectId:'',
                subjectParentId:'',
                title:''
            },//条件查询对象
            total: 0, //总记录数
            subjectNestedList:[],//一级课程分类
            teacherList:[],//讲师数据
            subsubjectList:[],//二级课程分类
        }
    },
    created() {
        // 获取课程分类分页数据
        this.getList()
        // 获取一级课程分类数据
        this.initSubjectList()
        // 获取讲师基本数据
        this.initTeacherList()
    },
    methods: {
        getList(page = 1) {
            // 获取当前页码
            this.page = page
            this.listLoading = true
            //调用获取讲师列表的api
            courseApi.getList(this.page, this.limit, this.searchQuery).then(res => {
                console.log(res);
                // 获取api中的数据
                this.list = res.data.items
                // 获取总记录数
                this.total = res.data.totalCount
                // 让菊花停止转动
                this.listLoading = false
            })
        },
        // 清空
        resetData() {
            // 将查询表单的对象属性清空
            this.teacherQuery = {}
            // 调用查询将是列表的api
            this.getList()
        },
        // 初始化课程分类下拉列表数据
        initSubjectList(){
            // 调用获取课程分类api
            subjectApi.getList().then(res=>{
                this.subjectNestedList=res.data
            })
        },
        // 初始化讲师下拉列表数据
        initTeacherList(){
            // 调用获取课程分类api
            teacherApi.listTeacher().then(res=>{
                this.teacherList=res.data
            })
        },
        // 一级课程分类改变时触发
        subjectLevelOneChange(value){
            // 循环遍历yijifenlei
            for(let i=0;i<this.subjectNestedList.length;i++){
                // 如果当前获取到的id等于一级课程下的id
                if(this.subjectNestedList[i].id==value){
                    // 把当前一级课程下的子集赋给二级课程
                    this.subsubjectList=this.subjectNestedList[i].children
                    // 清空查询条件中的二级分类id
                    this.searchQuery.subjectId=''
                }
            }
        },
        // 删除
        deleteCourse(id) {
            console.log(id);
            this.$confirm('您确定要删除该课程吗?', '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                courseApi.deleteCourse(id).then(res => {
                    // 删除成功提示消息
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                    // 重新加载数据
                    this.getList()
                });
            }).catch(() => {

            });
        }
    }
}
</script>
<style scoped>
.jslb {
    color: #A25900;
    font-size: 20px;
    font-weight: 800;
    line-height: 40px;
}
.myClassList .info {
    width: 300px;
    overflow: hidden;
}
.myClassList .info .pic {
    width: 150px;
    height: 90px;
    overflow: hidden;
    float: left;
}
.myClassList .info .pic a {
    display: block;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.myClassList .info .pic img {
    display: block;
    width: 100%;
}
.myClassList td .info .title {
    
    float: left;
    margin-left: 20px;
    height: 90px;
}
.myClassList td .info .title a {
    display: block;
    height: 48px;
    line-height: 24px;
    overflow: hidden;
    color: #00baf2;
    margin-bottom: 12px;
}
.myClassList td .info .title p {
    line-height: 20px;
    margin-top: 5px;
    color: #818181;
}
</style>